//
//  MHCircleTagView.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/4/17.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHCircleTagView.h"

@interface MHCircleTagView()
@property(nonatomic, strong)NSArray <MHCircleTag *>* circleTags;
@property(nonatomic, assign) int  preSelectIndex;
@property(nonatomic, strong) NSMutableArray *buttons;
@end

@implementation MHCircleTagView

-(instancetype)initWithTags:(NSArray <MHCircleTag *>*) tags
{
    self = [super init];
    if (self) {
        self.circleTags = tags;
        self.backgroundColor = [UIColor whiteColor];
        self.preSelectIndex = -1;
        self.buttons = [NSMutableArray arrayWithCapacity:tags.count];
        [self masLayoutSubViews];
        
        
    }
    return self;
}

-(void)bindViewModel:(id)viewModel
{

}


-(void)setSelectIndex:(int)selectIndex
{
    for (MHCircleTag *circleTag in self.circleTags) {
        if(circleTag.autoId == selectIndex)
        {
            _selectIndex = selectIndex;
           NSUInteger index = [self.circleTags indexOfObject:circleTag];
                if (index >= 0 && index < self.buttons.count) {
                    UIButton *button  = self.buttons[index];
                    button.selected = YES;
                }
        }
    }
//    if (selectIndex >= 0 && selectIndex < self.buttons.count) {
//        _selectIndex  = selectIndex;
//        UIButton *button  = self.buttons[_selectIndex];
//        button.selected = YES;
//    }
}

- (void)masLayoutSubViews
{
    CGFloat space = 30;
    CGFloat hSpace = 15;
    CGFloat w = (MH_SCREEN_WIDTH-(space*5))/4;
    int rows = ceilf(self.circleTags.count/4.0);
    for (int j = 0;j<rows;j++) {
        for (int i = 0; i<4; i++) {
            int index = j*4+i;
            if (index >= self.circleTags.count) {
                return;
            }
            MHCircleTag *tag = self.circleTags[index];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i*w+((i+1)*space), j*w+((j+1)*hSpace), w, w );
            [button setTitleColor:MH_MAIN_TEXT_COLOR_82 forState:0];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
            [button setBackgroundImage:[UIImage imageNamed:@"circle_tag_normal"] forState:0];
            [button setBackgroundImage:[UIImage imageNamed:@"circle_tag_selected"] forState:UIControlStateSelected];
//            button.layer.cornerRadius = w/2;
            __weak __typeof(self)weakSelf = self;
            [button bk_whenTapped:^{
                for (UIButton *tagButton in weakSelf.buttons) {
                    if (tagButton != button) {
                        tagButton.selected = false;
                    }
                }
                button.selected = YES;
                if (weakSelf.selectTagBlock) {
//                     weakSelf.selectIndex = index;
                     weakSelf.selectTagBlock(tag.autoId);
                }
            }];
            button.layer.masksToBounds = YES;
            button.titleLabel.font = UIFontFromPixel(24);
//            button.layer.borderWidth = 1.f;
//            button.layer.borderColor = MH_MAIN_TEXT_COLOR_82.CGColor;
            [button setTitle:tag.attrVal forState:0];
            [self.buttons addObject:button];
            [self addSubview:button];
            
        }
    }
    
    
}
@end
